package com.fly.controller;

import com.fly.bean.*;
import com.fly.service.AddFixTaskService;
import com.fly.util.Msg;
import com.fly.vo.AddFix_WH;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
public class AddFixTaskController {

    @Resource
    private AddFixTaskService addFixTaskService;


    //查询所有的工作表单类型
    @RequestMapping("/selectAllWorkForm")
    @ResponseBody
    public Msg selectWorkForm() {

        List<WorkForm> list = addFixTaskService.selectAllWorkForm();

        return Msg.success().add("workForm", list);
    }

    //查询所有的线路管理员用户 开启状态的
    @RequestMapping("/selectAllLineUser")
    @ResponseBody
    public Msg selectLineUser() {

        List<Users> list = addFixTaskService.selectAllLineUser();

        return Msg.success().add("users", list);
    }

    //查询所有的消缺管理员用户 开启状态的
    @RequestMapping("/selectAllFixUser")
    @ResponseBody
    public Msg selectFixUser() {

        List<Users> list = addFixTaskService.selectAllFixUser();

        return Msg.success().add("users", list);
    }

    //查询所有的电线杆的缺陷情况  未解决的
    @RequestMapping("/selectPoleDamage")
    @ResponseBody
    public Msg selectPoleDamage() {

        List<DamageRecord> list = addFixTaskService.selectPoleDamage();

        return Msg.success().add("damageRecord", list);
    }


    //添加消缺任务
    @RequestMapping("/addFixTask")
    @ResponseBody
    public Msg addFixTask(Task task, Users users, String workFormId, String damageRecordId, String userId, String fixUserId, String manageUserId) {

        AddFix_WH addFix_wh = new AddFix_WH(workFormId, task, users, damageRecordId, userId, fixUserId, manageUserId);

        SystemPropertiesValue systemPropertiesValue = new SystemPropertiesValue();
        SystemProperties systemProperties = new SystemProperties();
        systemProperties.setSysProId(4);
        systemPropertiesValue.setSystemProperties(systemProperties);
        //定义状态码对应的配置Id
        int id = 0;
        //false代表未分配true代表已分配
        boolean falg = false;

        //通过判断是否分配消缺员 来确定配置Id
        if (addFix_wh.getFixUserId() == "") {
            systemPropertiesValue.setSysProValueId(1);
            id = addFixTaskService.selectIdByState(systemPropertiesValue);
            falg = false;
        } else {
            systemPropertiesValue.setSysProValueId(2);
            id = addFixTaskService.selectIdByState(systemPropertiesValue);
            falg = true;
        }
        addFix_wh.getSystemPropertiesValue().setId(id);

        //添加任务
        int count1 = addFixTaskService.addTask(addFix_wh);

        //查询添加的任务Id
        int taskId = addFixTaskService.selectTaskByTaskNo(addFix_wh);
        addFix_wh.getTask().setTaskId(taskId);

        //添加消缺记录
        int count2 = addFixTaskService.addFix(addFix_wh);

        //查询添加的消缺记录Id
        int fixId = addFixTaskService.selectFixByTaskId(addFix_wh);
        addFix_wh.setFixId(fixId);

        //falg为true则添加任务与用户的关系 即分配任务给消缺员
        int count3 = 0;
        if (falg == true) {
            String[] fixUserIds = addFix_wh.getFixUserId().split(",");
            for (String s : fixUserIds) {
                addFix_wh.getFixUserIds().add(Integer.valueOf(s));
            }
            count3 = addFixTaskService.addTaskUserRelation(addFix_wh);
        }

        //添加电线杆与消缺任务的关系 即添加的消缺任务对应有哪些电线杆
        addFix_wh.setDamageState(1);
        String[] poleIds = addFix_wh.getDamageRecordId().split(",");
        for (String s : poleIds) {
            addFix_wh.setDamageRecordId(s);
            addFixTaskService.addPoleFixRelation(addFix_wh);
        }

        //修改电线杆损害状态由为解决改为为已分配
        addFixTaskService.updatePoleDamageState(addFix_wh);


        return Msg.success().add("msg",
                "成功添加任务成功,Id为:" + taskId + "       条数为:" + count1 +
                        "       成功添加消缺任务成功,Id为:" + fixId + "     条数为:" + count2 +
                        "       成功添加任务与用户的关系" + count3 + "条       " +
                        "成功添加电线杆与消缺任务的关系" + poleIds.length + "条" +
                        "       修改电线杆状态成功 " + poleIds.length + "条");
    }

//    查询该消缺任务的未解决的缺陷
    @RequestMapping("/selectPoleDamageByFixId")
    @ResponseBody
    public Msg selectPoleDamageByFixId(int fixId) {

        List<DamageRecord> list = addFixTaskService.selectPoleDamageByFixId(fixId);

        return Msg.success().add("damageRecord", list);
    }
}
